﻿using System;
using System.Web;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;

namespace ShopOnline
{
    public class WriteLog
    {
        public static string LogPath
        {
            get { return ConfigurationSettings.AppSettings.Get("LogPath"); }
        }

        public static void WriteErrorLog(string pUser, string pBehavior, string pStatus, string pIP, string pOther)
        {
            System.DateTime vDateNow = new System.DateTime();
            string vFolderName = LogPath + "Errors" + "\\" + pBehavior + "\\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00");
            string vFileName = vFolderName + "\\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00") + ".txt";
            System.IO.StreamWriter objStreamWriter = null;
            string vStringLog = System.DateTime.Now.ToString() + ", " + pUser + ", " + pBehavior + ", " + pStatus + ", " + pIP;

            try
            {
                if (!System.IO.Directory.Exists(vFolderName))
                {
                    System.IO.Directory.CreateDirectory(vFolderName);
                }

                if (!System.IO.File.Exists(vFileName))
                {
                    StreamWriter objFile = System.IO.File.CreateText(vFileName);
                    objFile.Close();
                }

                if (!string.IsNullOrEmpty(pOther))
                {
                    vStringLog += ", " + pOther;
                }

                objStreamWriter = System.IO.File.AppendText(vFileName);
                objStreamWriter.WriteLine(vStringLog);
                objStreamWriter.Close();

            }
            catch (Exception ex)
            {
            }
        }
        //
        public static void WriteActionLog(string pUser, string pBehavior, string pStatus, string pIP, string pOther)
        {
            System.DateTime vDateNow = new System.DateTime();
            string vFolderName = LogPath + pBehavior + "\\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00");
            string vFileName = vFolderName + "\\" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00") + ".txt";
            System.IO.StreamWriter objStreamWriter = null;
            string vStringLog = System.DateTime.Now.ToString() + ", " + pUser + ", " + pBehavior + ", " + pStatus + ", " + pIP;

            try
            {
                if (!System.IO.Directory.Exists(vFolderName))
                {
                    System.IO.Directory.CreateDirectory(vFolderName);
                }

                if (!System.IO.File.Exists(vFileName))
                {
                    StreamWriter objFile = System.IO.File.CreateText(vFileName);
                    objFile.Close();
                }

                if (!string.IsNullOrEmpty(pOther))
                {
                    vStringLog += ", " + pOther;
                }

                objStreamWriter = System.IO.File.AppendText(vFileName);
                objStreamWriter.WriteLine(vStringLog);
                objStreamWriter.Close();

            }
            catch (Exception ex)
            {
            }
        }
        //
        /// <summary>
        /// Write Log With Flag Error -1: Error , 0: OK, 1: FAIL
        /// </summary>
        /// <param name="iFlagError">-1: Error , 0: OK, 1: FAIL</param>
        /// <param name="pUser"></param>
        /// <param name="pBehavior"></param>
        /// <param name="pIP"></param>
        /// <param name="pOther"></param>
        /// <remarks></remarks>
        public static void WriteLogWithFlag(Int32 iFlagError, bool WriteWhenOK, string pUser, string pBehavior, string pIP, string pOther)
        {
            if (iFlagError == -1)
            {
                WriteErrorLog(pUser, pBehavior, "ERROR", pIP, pOther);
            }
            else if (iFlagError == 0 && WriteWhenOK == true)
            {
                WriteActionLog(pUser, pBehavior, "OK", pIP, pOther);
            }
            else if (iFlagError == 1)
            {
                WriteActionLog(pUser, pBehavior, "FAIL", pIP, pOther);
            }
        }

    }
}
